package com.ruoyi.purchase.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.purchase.mapper.PurchaseApplyMapper;
import com.ruoyi.purchase.mapper.PurchaseApplyMaterialMapper;
import com.ruoyi.purchase.pojo.PurchaseApply;
import com.ruoyi.purchase.pojo.PurchaseApplyMaterial;
import com.ruoyi.purchase.service.PurchaseApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 采购申请Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-25
 */
@Service
public class PurchaseApplyServiceImpl implements PurchaseApplyService
{
    @Autowired
    private PurchaseApplyMapper purchaseApplyMapper;

    @Autowired
    private PurchaseApplyMaterialMapper purchaseApplyMaterialMapper;

    /**
     * 查询采购申请
     *
     * @param id 采购申请主键
     * @return 采购申请
     */
    @Override
    public PurchaseApply selectPurchaseApplyById(Integer id)
    {
        return purchaseApplyMapper.selectPurchaseApplyById(id);
    }

    /**
     * 查询采购申请列表
     *
     * @param purchaseApply 采购申请
     * @return 采购申请
     */
    @Override
    public List<PurchaseApply> selectPurchaseApplyList(PurchaseApply purchaseApply)
    {
        return purchaseApplyMapper.selectPurchaseApplyList(purchaseApply);
    }

    /**
     * 获取status=2的值
     * @param purchaseApply
     * @return
     */
    @Override
    public List<PurchaseApply> selectApplyAll(PurchaseApply purchaseApply) {
        return purchaseApplyMapper.selectApplyAll(purchaseApply);
    }

    /**
     * 修改状态
     * @param purchaseApply
     * @return
     */
    @Override
    public int updateStatus(PurchaseApply purchaseApply) {
        purchaseApply.setUpdateTime(DateUtils.getNowDate());
        return purchaseApplyMapper.updatePurchaseApply(purchaseApply);
    }
    /**
     * 新增采购申请
     *
     * @param purchaseApply 采购申请
     * @return 结果
     */
    @Transactional
    @Override
    public void insertPurchaseApply(PurchaseApply purchaseApply)
    {
        purchaseApply.setCreateTime(DateUtils.getNowDate());
        purchaseApplyMapper.insertPurchaseApply(purchaseApply);

        for (PurchaseApplyMaterial purchaseApplyMaterial : purchaseApply.getList()){
            purchaseApplyMaterial.setPurchaseApplyId(purchaseApply.getId());
            purchaseApplyMaterialMapper.insertPurchaseApplyMaterial(purchaseApplyMaterial);
        }
    }

    /**
     * 修改采购申请
     *
     * @param purchaseApply 采购申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePurchaseApply(PurchaseApply purchaseApply)
    {

        for (PurchaseApplyMaterial purchaseApplyMaterial : purchaseApply.getList()){
            if (purchaseApplyMaterial.getId() != null){
                purchaseApplyMaterial.setPurchaseApplyId(purchaseApply.getId());
                purchaseApplyMaterialMapper.updatePurchaseApplyMaterial(purchaseApplyMaterial);
            }else {
                purchaseApplyMaterial.setPurchaseApplyId(purchaseApply.getId());
                purchaseApplyMaterialMapper.insertPurchaseApplyMaterial(purchaseApplyMaterial);
            }
        }
        purchaseApply.setUpdateTime(DateUtils.getNowDate());
        return purchaseApplyMapper.updatePurchaseApply(purchaseApply);
    }

    /**
     * 批量删除采购申请
     *
     * @param ids 需要删除的采购申请主键
     * @return 结果
     */
    @Override
    public int deletePurchaseApplyByIds(Integer[] ids)
    {
        return purchaseApplyMapper.deletePurchaseApplyByIds(ids);
    }

    /**
     * 删除采购申请信息
     *
     * @param id 采购申请主键
     * @return 结果
     */
    @Override
    public int deletePurchaseApplyById(Integer id)
    {
        return purchaseApplyMapper.deletePurchaseApplyById(id);
    }

    @Override
    public int selectLastId() {
        return purchaseApplyMapper.selectLastId();
    }
}
